package com.adams.dcxt.core;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Config {

    private static Logger logger = LoggerFactory.getLogger(Config.class);

    private static Config instance = null;

    private static Configuration configuration = null;// 这里采用Configuration以便以后扩展XML等其他配置文件

    private static final String SYSTEM_CONFIG_FILE_NAME = "config.properties";// 系统配置文件名称

    private Config() {
        load();
    }

    public static synchronized Config getInstance() {
        if (instance == null) {
            synchronized (Config.class) {
                if (instance == null) {
                    instance = new Config();
                }
            }
        }
        return instance;
    }

    public Configuration getConfiguration() {
        return configuration;
    }

    /**
     * 
     * Description : 提供几个获取配置信息的常用方法
     * 
     * @param key
     * @return
     * 
     */
    public String getString(String key) {
        return configuration.getString(key);
    }


    private void load() {
        String filePath =  SYSTEM_CONFIG_FILE_NAME;

        // 读配置文件
        try {
            configuration = new PropertiesConfiguration(filePath);
            if (configuration == null) {
                logger.error("系统配置文件" + SYSTEM_CONFIG_FILE_NAME + "不存在!");
                throw new IllegalArgumentException("系统配置文件" + SYSTEM_CONFIG_FILE_NAME + "不存在!");
            }
        } catch (ConfigurationException e) {
            // TODO Auto-generated catch block
            logger.error("读系统配置文件" + SYSTEM_CONFIG_FILE_NAME + "出现错误!");
            throw new IllegalArgumentException("读系统配置文件" + SYSTEM_CONFIG_FILE_NAME + "出现错误!");
        }

    }

   
}
